package com.geektime.hello.service.impl;

import com.geektime.hello.model.Coupon;
import com.geektime.hello.repository.CouponRepository;
import com.geektime.hello.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponRepository couponRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Coupon> findCouponList() {
        return couponRepository.findAll();
    }

    @Override
    public List<Coupon> findCouponListByHql() {
        String hql = " from Coupon ";
        List<Coupon> coupons = entityManager.createQuery(hql).getResultList();
        return coupons;
    }

    /**
     * 和上面的JPA代码不同的是，返回的字段是数据库列名create_time，而上面返回的字段是属性名createTime
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findCouponList4Jdbc() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from t_coupon");
        //复杂还不对，我恁
//        return jdbcTemplate.queryForList(" select * from t_coupon ", new RowMapper() {
//            @Override
//            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
//                Coupon coupon = new Coupon();
//                System.out.println(rs.toString());
//                coupon.setId(Integer.parseInt(rs.getString(0)));
//                coupon.setCode(rs.getString(1));
//                coupon.setPicUrl(rs.getString(2));
//                coupon.setAchieveAmount(Integer.parseInt(rs.getString(3)));
//                coupon.setReduceAmount(Integer.parseInt(rs.getString(4)));
//                coupon.setStock(Integer.parseInt(rs.getString(5)));
//                coupon.setTitle(rs.getString(6));
//                coupon.setStatus(Integer.parseInt(rs.getString(7)));
//                coupon.setCreateTime(new Date(rs.getString(8)));
//                return coupon;
//            }
//        });

        //根据3y改写的
        List<Coupon> coupons = jdbcTemplate.query(" select * from t_coupon ", new RowMapper<Coupon>() {
            @Override
            public Coupon mapRow(ResultSet rs, int rowNum) throws SQLException {
                Coupon coupon = new Coupon();
                System.out.println(rs.toString());
                coupon.setId(Integer.parseInt(rs.getString(0)));
                coupon.setCode(rs.getString(1));
                coupon.setPicUrl(rs.getString(2));
                coupon.setAchieveAmount(Integer.parseInt(rs.getString(3)));
                coupon.setReduceAmount(Integer.parseInt(rs.getString(4)));
                coupon.setStock(Integer.parseInt(rs.getString(5)));
                coupon.setTitle(rs.getString(6));
                coupon.setStatus(Integer.parseInt(rs.getString(7)));
                coupon.setCreateTime(new Date(rs.getString(8)));
                return coupon;
            }
        });
        return list;
    }
}
